package com.ropeok.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ropeok.domain.User;
import com.ropeok.mapper.UserMapper;
import com.ropeok.service.UserService;
import com.ropeok.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @BelongsProject: training-mission
 * @BelongsPackage: com.ropeok.service.impl
 * @Author: zhofeifan
 * @CreateTime: 2022-09-26  14:36
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public void insertUser(User user) {
        //转换
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        userMapper.insert(user);
    }
    @Override
    public User findById(long id) {
        return userMapper.selectById(id);
    }
    @Override
    public List<User> findAll() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        return userMapper.selectList(wrapper);
    }
    @Override
    public Page<User> selectPage(Page<User> page, Object o) {
        Page<User> userPage = userMapper.selectPage(page, null);
        page.getRecords().forEach(System.out::println);
        return userPage;
    }
    @Override
    public Page findByPage(Integer pageSize, Integer pageNumber) {
        Page<User> page = new Page<>(pageSize,pageNumber);
        return userMapper.selectPage(page,null);
    }
    @Override
    public void deleteUserById(long id) {
        userMapper.deleteById(id);
    }
    @Override
    public void updateUser(User user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        userMapper.updateById(user);
    }
}
